/**
 * Created by Lannis on 2017/4/24.
 */
var Tools = (function () {
    function Tools() {
    }
    /**
     *生成指定范围内的随机数
     * @param minNumber 最小的数
     * @param maxNumber 最大的数
     * @param isRound   可选参数,如果为true,会产生指定范围内的整数随机数;false会产生指定范围内的浮点数.
     * @returns {any} 生成的随机数
     */
    Tools.getRandomBetween = function (minNumber, maxNumber, isRound) {
        var Range = maxNumber - minNumber;
        var Rand = Math.random();
        var num;
        if (isRound) {
            num = minNumber + Math.round(Range * Rand);
        }
        else {
            num = minNumber + Range * Rand;
        }
        return num;
    };
    /**
     * 生成指定长度的字符串
     * @param length 字符串长度、默认为32
     * @returns {string} 生成的随机数字符串
     */
    Tools.getRandomCharacters = function (length) {
        var len = length || 32;
        var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
        var maxPos = $chars.length;
        var pwd = '';
        for (var i = 0; i < len; i++) {
            pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
    };
    /**
     * 扩展对象参数，参数形式必须是对象。
     * @param defaultConfig 默认参数对象
     * @param newConfig 传入的参数对象
     */
    Tools.prototype.extendParameters = function (defaultConfig, newConfig) {
        for (var i in newConfig) {
            defaultConfig[i] = newConfig[i];
        }
    };
    /**
     * 对象/数组深度拷贝
     * @param toBeCopiedArray 要复制的数组/对象
     * @returns {any} 返回复制过后的数组
     */
    Tools.deepCopy = function (toBeCopiedArray) {
        if (toBeCopiedArray instanceof Array) {
            var n = [];
            for (var i = 0; i < toBeCopiedArray.length; ++i) {
                n[i] = this.deepCopy(toBeCopiedArray[i]);
            }
            return n;
        }
        else if (toBeCopiedArray instanceof Object) {
            var n = {};
            for (var i in toBeCopiedArray) {
                n[i] = this.deepCopy(toBeCopiedArray[i]);
            }
            return n;
        }
        else {
            return toBeCopiedArray;
        }
    };
    /**
     * 扩展Ajax加载js文件的插件：E.g
     $.cachedScript('js/xxx.js').done(function () {
                         console.log(scriptsArray);//回调函数，上面xxx.js文件加载成功后才执行的方法
                     });
     */
    Tools.extendJqueryLoadJsFunc = function () {
        window['scriptsArray'] = window['scriptsArray'] || new Array(); //定义一个全局script的标记数组，用来标记是否某个script已经下载到本地
        if (!window.hasOwnProperty("$")) {
            console.log("404:Jquery not found!");
            return;
        }
        window["$"].cachedScript = function (url, options) {
            for (var s in window['scriptsArray']) {
                if (window['scriptsArray'][s] == url) {
                    return {
                        done: function (method) {
                            if (typeof method == 'function') {
                                method();
                            }
                        }
                    };
                }
            }
            options = window["$"].extend(options || {}, {
                dataType: "script",
                url: url,
                cache: true
            });
            window['scriptsArray'].push(url); //将url地址放入script标记数组中
            return window["$"].ajax(options);
        };
    };
    Tools.getAngle = function (x1, y1, x2, y2) {
        // 直角的边长
        var x = Math.abs(x1 - x2);
        var y = Math.abs(y1 - y2);
        // 斜边长
        var z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
        // 余弦
        var cos = y / z;
        // 弧度
        var radina = Math.acos(cos);
        // 角度
        var angle = 180 / (Math.PI / radina);
        return angle;
    };
    Tools.getAngle2 = function (p1, p2) {
        var k1 = (p2.y - p1.y) / (p2.x - p1.x);
        var k2 = 0;
        var theta = Math.abs((k2 - k1) / (1 + k1 * k2));
        return Math.atan(theta);
    };
    Tools.prototype.RectToPolar = function (x, y) {
        var r; // 极坐标半径
        var B; // 极坐标夹角
        r = Math.hypot(x, y);
        if (y >= 0) {
            if (x == 0) {
                B = Math.PI / 2; // 90°
            }
            else {
                B = Math.asin(x / y);
            }
        }
        else if (y < 0) {
            if (x == 0) {
                B = 3 * Math.PI / 2; // 270°
            }
            else {
                B = Math.asin(x / y);
            }
        }
    };
    return Tools;
}());
